const {getDmg,reduceResistanceK,lvSuppressK,objValueAdd } = require("../libs/apis.js")

const WEAPONS = require("./weapons.js");

const CLIPDATA_TYPE = {
    max:{
        fixedhp: 299,
        fixedAtt: 19,
        fixdefence: 23,
        reaction : 23,
        attRate: 5.8, 
        hpRate: 5.8, 
        criticalRate: 3.9,
        criticalStrike: 7.8,
        defRate: 7.3,
        charging: 6.5
    },
    mid:{
        fixedhp: 254,   // 209  239   269   299
        fixdefence: 19.75, // 16   19  21  23
        fixedAtt: 16.75,   //  14   16   18  19
        reaction : 19.75,  // 16  19   21   23
        attRate: 4.957,  // 4.1   4.7   5.3  5.8
        hpRate: 4.957,   // 4.1  4.7  5.3  5.8
        criticalRate: 3.3,   //  2.7  3.1   3.5   3.9
        criticalStrike: 6.6,  //  5.4  6.2  7  7.8
        defRate: 6.2,   //  5.1  5.8  6.6   7.3
        charging: 5.5  //  4.5   5.2   5.8   6.5
    }
}

const CLIPDATA = CLIPDATA_TYPE.max

const CONFIG = {
    chance: 25,
    your_lv: 90,
    enemy_lv: 90,
    clips_main: {
        criticalStrike: 62.2,
        dmgRate: 46.6,
        // attRate: 46.6,
        charging: 51.8
    },
    clips: ["charging", "reaction", "criticalRate", "criticalStrike"],
    clips_initial: [
        ["reaction", "attRate", "criticalRate", "criticalStrike"],
        ["reaction", "attRate", "criticalRate", "criticalStrike"],
        ["attRate", "reaction", "criticalRate", "criticalStrike"],
        ["reaction", "attRate", "criticalRate", "criticalStrike"],
        ["charging", "reaction", "attRate", "criticalRate"]
    ],
    reactionK : 1.5,
    buffs: [  
        {dmgRate: 15}, // q
        // {charging: 20}, // 充能
        {dmgRate: 22.5, reactionDmgAdd: 0.15,}, // 魔女
        // {reaction: 80 + 50*2, attRate: 14}, // 新套
        {attRate: 20 + 25,fixedAtt: 1203}, // 班尼特
        {attRate: 20, dmgRate: 40, jk:40}, // 万叶
    ]
}
// atk  4150  dmgRate:158.1   cs:178.2   2.38    1.225    0.5
let charaters = {
    units : {
        name: "香菱",
        reaction: 96,
        charging: 100,
        defRate: 0,
        defence: 669,
        qDmgAdd:0,
        hp:10875,
        base: 225,    
        criticalRate: 5,    
        criticalStrike: 50,
        skills: {
            a: [
                function(data){},
            ]
        }
    },
}
// 香菱 80 能量q   万达: 60*3+80 = 260 断浪增伤 62.4
// 雷行香班
/**
 * 充能沙 断浪82.1 决斗85 灭辰77.65 鱼叉 : 76.3  雷枪78.7
 * 攻击沙 鱼叉81.32 雷神枪80.9
 * 
以下伤害为数学期望伤害 也就是考虑了暴击率后的平均伤害
毕业标准副词条分配是 初始4词条 强化4次 歪一次无效 词条数值为平均值
副词条分配比例由程序算法分配期望伤害最优比

万达体系 公子万叶香菱班尼特
吃拐: 班尼特宗室20攻 1202固定攻击 双火25攻 万叶减火抗40 1精仓古20攻 扩散火伤40 
  绝缘套 (充能沙)
      鱼叉(5精)  q 29746     q蒸发 59767  充能:223  
      灭辰(5精)  q 25676     q蒸发 63029  充能:177  
      断浪(5精)  q 32405     q蒸发 65110  充能:177  
      决斗对单(5精)  q 32232     q蒸发 64761  充能:177  
     
  绝缘套 (攻击沙) 
      鱼叉(5精)  q 31561     q蒸发 63414  充能:177  

  魔女套 (攻击沙) 
      鱼叉(5精)  q 29093     q蒸发 65001  充能:157  

  魔女套 (充能沙) 
      决斗对单(5精)  q 29371     q蒸发 65623  充能:157  
      断浪枪(5精)  q 30111     q蒸发 67276  充能:157  

  1精绿枪如果叠满特效会比这些伤害要高 但站场花时间叠特效浪费时间不实用
  个人比较推荐的是绝缘套 充能沙的鱼叉 200+充能才舒服 香菱循环舒服才是王道
 */
calc(calc_clips_initial());

function calc(normal) {
    let charactersDmg = [];
    let count = 0;
    Object.values(WEAPONS).forEach(weapon => {
        if(weapon.weapontype == "qiang") {
            charactersDmg.push(getDMG([charaters.units,weapon,{fixedAtt:311,...CONFIG.clips_main},normal], (data,second) => {
                count++;
                data = objValueAdd([data, ...CONFIG.buffs]);
                
                if(weapon.skill) { weapon.skill(data); }
                
                let rea = { reaction:data.reaction, reactionQ:CONFIG.reactionK, reactionDmgAdd:(data.reactionDmgAdd||0) }
     
                let slipQadd = data.charging * 0.25
                if(slipQadd > 75) {
                    slipQadd = 75
                }
                // data.qDmgAdd += slipQadd;
                // 13 e    236.47  12s qDmgAdd
                // 13 q    153  187  232.9    238 * 13
                let EDmg = getDmg({ data, rate:2.3647}).qw * reduceResistanceK(-45) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
                let EDmgRea = getDmg({ data, rate:2.3647, rea}).qw * reduceResistanceK(-45) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
                data.criticalRate += (data.qBj||0)
                let QDmh1 = getDmg({ data:{...data, dmgRate: data.dmgRate + (data.qDmgAdd||0)}, rate:1.53}).qw * reduceResistanceK(-5-(data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
                let QDmh1Rea = getDmg({ data:{...data, dmgRate: data.dmgRate + (data.qDmgAdd||0)}, rate:1.53, rea}).qw * reduceResistanceK(-5-(data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
                let QDmh2 = getDmg({ data:{...data, dmgRate: data.dmgRate + (data.qDmgAdd||0)}, rate:1.87}).qw * reduceResistanceK(-5-(data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
                let QDmh2Rea = getDmg({ data:{...data, dmgRate: data.dmgRate + (data.qDmgAdd||0)}, rate:1.87, rea}).qw * reduceResistanceK(-5-(data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
                let QDmh3 = getDmg({ data:{...data, dmgRate: data.dmgRate + (data.qDmgAdd||0)}, rate:2.329}).qw * reduceResistanceK(-5-(data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
                let QDmh3Rea = getDmg({ data:{...data, dmgRate: data.dmgRate + (data.qDmgAdd||0)}, rate:2.329, rea}).qw * reduceResistanceK(-5-(data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
                let QDmhn = getDmg({ data:{...data, dmgRate: data.dmgRate + (data.qDmgAdd||0)}, rate:2.38}).qw * reduceResistanceK(-5-(data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
                let QDmhnRea = getDmg({ data:{...data, dmgRate: data.dmgRate + (data.qDmgAdd||0)}, rate:2.38, rea}).qw * reduceResistanceK(-5-(data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
                
                let QDmhnWuGuoBa = getDmg({ data:{...data, dmgRate: data.dmgRate + (data.qDmgAdd||0)}, rate:2.38}).qw * reduceResistanceK(10-(data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
                let QDmhnReaWuGuoBa = getDmg({ data:{...data, dmgRate: data.dmgRate + (data.qDmgAdd||0)}, rate:2.38, rea}).qw * reduceResistanceK(10-(data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
                
                let aft = data.reaction * 16 / (2000 + data.reaction)
    
                let chaozai =  723 * 4 * (aft + 1) * 0.9
                // if(data.phyRate) { // 物理特效伤害
                //     data.phyDmg = getDmg({data:{...data, dmgRate:0}, rate:data.phyRate}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);  // 
                // }
    
                let point = EDmg * 2 + EDmgRea + chaozai * 0 +  QDmh1Rea + QDmh2 + QDmh3 + 
                QDmhnWuGuoBa*3 + QDmhnReaWuGuoBa*4 + QDmhn * 3 + QDmhnRea * 3;
                let attack = data.base *(1 + data.attRate/100) + data.fixedAtt;
                return {point, data, second, attack, dmgs:{EDmg,EDmgRea,QDmh1,QDmh1Rea,QDmh2,QDmh2Rea,QDmh3,QDmh3Rea,QDmhn,QDmhnRea,chaozai}}
            }))
        }
    });
    charactersDmg.sort((a,b)=>{
        return (b.point)-(a.point);
    });
    console.log(charactersDmg[9]);
    console.log(charactersDmg[10]);
    console.log(charactersDmg.map((v, n)=>{
        return  `(${n})得分: ${v.data.weaponname} : ${v.point.toFixed(0)}`
    }));
    console.log(count);
}

function getDMG(dataarr, getRet){
    let chance = CONFIG.chance;
    let _ret = [];
    for (let index = 0; index <= chance; index++) {
        let c1 = index; 
        for (let index2 = 0; index2 <= chance - index; index2++) {
            let c2 = index2;
            for (let index3 = 0; index3 <= chance-index-index2; index3++) {
                let c3 = index3;
                let c4 = chance - index - index2 - index3;
                let cs = [c1, c2, c3, c4];
                var tmpdata = {};
                CONFIG.clips.forEach((v, i) => {
                    tmpdata[v] = CLIPDATA[v] * cs[i]
                })
                _ret.push(tmpdata);
            }
        }
    }
    _ret = _ret.map(second => {
        let panel = objValueAdd([...dataarr, second]);
        return  getRet(panel, second)
    })
    _ret.sort((b,a)=> a.point-b.point)
    return _ret[0]
}

// 固定攻击力 19  防御力23 生命值299 5.3% 
function calc_clips_initial () {
    let clips = CONFIG.clips_initial.map(v => {
        let tmp = {};
        v.forEach(v2 => {
            tmp[v2] = CLIPDATA[v2]
        })
        return tmp
    })
    let data = {}
    clips.forEach(item => {
        for (const key in item) {
            data[key] = data[key]? (data[key]+item[key]) : item[key]
        }
    })
    return data
}
